import pathToRegexp from 'path-to-regexp/index';
import {
    getCategory,
    postCategory,
    getCategoryList,
    postCategoryListUp,
    getCategoryDown,
    postCategoryDown,
    deleteCategoryListUp,
    deleteCategoryDown,
    putCategoryDown,
} from 'services/app';
import { routerRedux } from 'dva/router';
export default {
    namespace: 'category',
    state: {
        pages:1,
        panes:[],
        record:{},
        activeKey:'000',
    },
    subscriptions: {
        setupHistory ({ dispatch, history }) {
            history.listen((location) => {
                const match = pathToRegexp('/home').exec(location.pathname);
                if(location.pathname == '/categorymanagement'){
                    dispatch({type:'getCategory',payload:{
                        pageNumber:1,
                        pageSize:10,
                        title_name:'',
                        title_type:'',
                    }});
                }
            });
        },
        setup ({ dispatch }) {},
    },
    effects: {
        //获取一级题目列表
        *getCategory ({ payload }, { call, put }){
            const {data,total} = yield call(getCategory, payload);
            yield put({ type: 'save', payload: {
                getCategoryData:data,
                total:total,
                activeKey:'000',
                pages:payload.pageNumber,
                visible:false,
                visibleTow:false,
            }});
        },
        //添加一级题目列表
        *postCategory ({ payload }, { call, put }){
            const data = yield call(postCategory, payload);
            if(data.code == 0){
                yield put({type:'getCategory',payload:{
                    pageNumber:1,
                    pageSize:10,
                    title_name:'',
                    title_type:'',
                }});
            }
        },
        /*---------题目目录操作--------*/
        //获取题目目录
        *getCategoryList ({ payload }, { call, put }){
            const {data} = yield call(getCategoryList, payload);
            yield put({type:'save',payload:{panes:data,visible:false}});
            if(data.length !== 0){
                yield put({type:'getCategoryDown',payload:{catalog_id:data[0].id}});
            }
        },
        //添加题目目录
        *postCategoryListUp ({ payload }, { call, put }){
            const data = yield call(postCategoryListUp, payload);
            if(data.code == 0){
                yield put({type:'getCategoryList',payload:{title_id:localStorage.getItem('id')}});
            }
        },
        //删除题目目录
        *deleteCategoryListUp ({ payload }, { call, put }){
            const data = yield call(deleteCategoryListUp, payload);
            if(data.code == 0){
                yield put({type:'getCategoryList',payload:{title_id:localStorage.getItem('id')}});
            }
        },
        /*---------题目操作--------*/
        //获取题目
        *getCategoryDown ({ payload }, { call, put }){
            const {data} = yield call(getCategoryDown, payload);
            yield put({type:'save',payload:{content:data,visibleTow:false,activeKey:payload.catalog_id}});
        },
        //添加题目
        *postCategoryDown ({ payload }, { call, put }){
            const data = yield call(postCategoryDown, payload);
            if(data.code == 0){
                yield put({type:'getCategoryDown',payload:{catalog_id:payload.catalog_id}});
            }
        },
        //修改题目
        *putCategoryDown ({ payload }, { call, put }){
            const data = yield call(putCategoryDown, payload);
            if(data.code == 0){
                yield put({type:'getCategoryDown',payload:{catalog_id:payload.catalog_id}});
            }
        },
        //删除题目
        *deleteCategoryDown ({ payload }, { call, put }){
            const data = yield call(deleteCategoryDown, payload);
            if(data.code == 0){
                yield put({type:'getCategoryDown',payload:{catalog_id:payload.catalog_id}});
            }
        },
    },
    reducers: {
        save (state, { payload }) {
            return { ...state, ...payload };
        },
    },
};
